<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        管理
        <small>列表</small>
    </h1>
</section>

<!-- Main content -->
<section class="content" id="start-page-app">

    <div class="row">
        <div class="col-md-6">
            <a class="btn btn-default" @click="getList">刷新</a>
            <a v-show="!show.edit" class="btn btn-info" @click="add">添加</a>
            <a v-show="show.edit" class="btn btn-danger" @click="show.edit = false">关闭</a>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label>选择页面</label>
            </div>
        </div>
    </div>

    <div class="row" v-show="!show.edit">
        <div class="col-md-12">
            <table class="table table-responsive table-striped table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>图片</th>
                    <th>排序</th>
                    <th>跳转</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="item in list">
                    <td>{{item.id}}</td>
                    <td>
                        <a class="avatar" :href="item.image" target="_blank">
                            <img :src="item.image">
                        </a>
                    </td>
                    <td>{{item.rank}}</td>
                    <td>{{item.has_link ? '是' : '否'}}</td>
                    <td>
                        <a class="btn btn-primary btn-xs" @click="edit(item)">编辑</a>

                        <a v-show="item.hide" class="btn btn-info btn-xs" @click="toggle(item)">显示</a>
                        <a v-show="!item.hide" class="btn btn-warning btn-xs" @click="toggle(item)">隐藏</a>

                        <a class="btn btn-danger btn-xs" @click="del(item)">删除</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="100">
                        <page :rows="page.rows" :last="page.last" v-model="page.page"></page>
                    </td>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <div class="row" v-show="show.edit">
        <div class="col-md-4 col-md-offset-2">
            <h3>编辑</h3>
            <div class="form" role="form">

                <div class="form-group">
                    <label>图片(750x380)</label>
                    <file-uploader v-model="edit_item.image" filetype="image" oss="true" policyUrl="/alioss/get_policy"></file-uploader>
                </div>

                <div class="form-group">
                    <label>点击跳转：<input type="checkbox" v-model="edit_item.has_link" value="1"></label>
                </div>

                <div class="form-group" v-show="edit_item.has_link">
                    <label>跳转地址：</label>
                    <input class="form-control" v-model="edit_item.link_url">
                </div>

                <div class="form-group">
                    <label>排序</label>
                    <input class="form-control" type="number" v-model="edit_item.rank" step="1">
                </div>

                <div class="form-group">
                    <label>隐藏：<input type="checkbox" v-model="edit_item.hide" value="1"></label>
                </div>

                <div class="form-group">
                    <a class="btn btn-primary" @click="save">保存</a>
                    <a class="btn btn-default" @click="show.edit = false">关闭</a>
                </div>
            </div>
        </div>
    </div>

</section>
<!-- /.content -->

<script>
    var start_page_app = new Vue({
        el: '#start-page-app',
        data: {
            list: [],
            edit_item: {},
            page: {
                last: 1,
                page: 1,
                rows: 10,
            },
            show: {
                edit: false
            }
        },
        created: function () {
            this.getList();
        },
        methods: {
            getList: function () {
                ajax({
                    uri: '/start_page/list',
                    data: this.page,
                    success: function(resp){
                        this.list = resp.data;
                        this.page.last = resp.last_page;
                    }.bind(this)
                });
            },
            add: function () {
                this.edit_item = {rank: 255,has_link:1};
                this.show.edit = true;
            },
            edit: function (item) {
                this.edit_item = item;
                this.show.edit = true;
            },
            save: function () {
                var item = this.edit_item;
                if (!item.image) {
                    alert('请上传图片！');
                    return;
                }

                ajax({
                    uri: '/start_page/save',
                    data: item,
                    success: function(resp){
                        this.show.edit = false;
                        this.getList();
                    }.bind(this)
                });
            },
            del: function (item) {
                ajax({
                    uri: '/start_page/del',
                    data: {id: item.id},
                    success: function (resp) {
                        this.getList();
                    }.bind(this)
                })
            },
            toggle: function (item) {
                var hide = item.hide ? 0 : 1;
                ajax({
                    uri: '/start_page/toggle_show',
                    data: {id: item.id, hide: hide},
                    success: function (resp) {
                        item.hide = hide;
                    }.bind(this)
                })
            },
            turnPage: function (offset) {
                this.page.offset = offset;
            }
        },
        filters: {
        },
        watch: {
            'page.page': function (nv, ov) {
                this.getList();
            }
        }
    });
</script>